#pragma once
#include <iostream>
#include <vector>
#include "log.hpp"
#include "inetaddr.hpp"
using namespace hllog;
using namespace hladdr;
namespace hlrounte
{
    class Rounte
    {
        bool IsExist(InetAddr &addr)
        {
            for (auto &user : _online_users)
            {
                if (user == addr)
                    return true;
            }
            return false;
        }

    public:
        Rounte() {}
        ~Rounte() {}
        void SendTask(int sockfd, const std::string &massage, InetAddr &peer)
        {
            if (IsExist(peer) == false)
            {
                _online_users.push_back(peer);
                LOG(Level::INFO) << "新增了一个在线用户";
            }
            // 发送信息
            std::string str = peer.ToString() + "# " + massage;
            for (auto &user : _online_users)
            {
                sendto(sockfd, str.c_str(), str.size(), 0, user.GetInetAddr(), user.GetLen());
            }
            if (massage == "QUIT")
            {
                LOG(Level::INFO) << peer.ToString() << "用户退出";
                auto pos = _online_users.begin();
                while (pos != _online_users.end())
                {
                    if (*pos == peer)
                        break;
                }
                _online_users.erase(pos);
            }
        }

    private:
        std::vector<InetAddr> _online_users;
    };
}